package com.twu.biblioteca;
import java.io.*;
import java.util.Formatter;

public class BibliotecaApp {
    String[] BookNames=new String[20];

    public static void main(String[] args) {
        String [] username={"111-1111","111-1112","111-1113","111-1114","111-1115"};
        String [] name= {"Anuja","Dilip","Parth","Aditi","Suman"};
        String [] pwd={"abc","def","ghi","jkl","mno"};
        String [] emailID={"u1@gmail.com","u2@gmail.com","u3@gmail.com","u4@gmail.com","u5@gmail.com"};
        String [] phno ={"9807767676","9434122321","9023104511","8140067811","8066732109"};
        String [] movName = {"The Godfather","The Forrest Gump","The Shawshank Redemption","The Green Mile",
                "Avatar","The Pursuit of Happyness","Million Dollar Baby","Cast Away","A Beautiful Mind",
                "Life of Pi","Inception","Pulp Fiction","Gladiator","Schindler's List","The Curious Case of Benjamin Button"};
        int [] yr={1972,1994,1994,1999,2009,2006,2004,2000,2001,2012,2010,1994,2000,1993,2008};
        String [] dir={"Francis Ford Coppola","Robert Zemeckis","Frank Darabont","Frank Darabont","James Cameron",
                "Gabriele Muccino","Clint Eastwood","Robert Zemeckis","Ron Howard","Ang Lee","Christopher Nolan",
                "Quentin Tarantino","Ridley Scott","Steven Spielberg","David Fincher"};
        String [] rating ={"9","8","N/A","7","9","N/A","8","9","9","8","N/A","N/A","8","9","7"};

        User u[]= new User[5];
        Movie m[]=new Movie[15];
        System.out.println("Welcome to Biblioteca!!");
        BibliotecaApp b = new BibliotecaApp();
        for(int i=0;i<5;i++)
        {
            u[i]=new User(username[i],name[i],pwd[i],emailID[i],phno[i]);
        }
        for(int i=0;i<15;i++)
        {
            m[i]=new Movie(movName[i],yr[i],dir[i],rating[i]);
        }
        b.menu(m,u);
        System.out.println("Thank you for using Biblioteca! Visit again...");
    }

    public BibliotecaApp()
    {
        BookNames[0]="Les Miserables";
        BookNames[1]="Game of Thrones";
        BookNames[2]="The Hobbit";
        BookNames[3]="The Great Gatsby";
        BookNames[4]="Gone Girl";
        BookNames[5]="Little Women";
        BookNames[6]="The Origin of Species";
        BookNames[7]="To Kill a Mockingbird";
        BookNames[8]="Pride and Prejudice";
        BookNames[9]="The Giving Tree";
        BookNames[10]="The Kite Runner";
        BookNames[11]="Brave New World";
        BookNames[12]="The Fountainhead";
        BookNames[13]="Memoirs of a Geisha";
        BookNames[14]="The Secret Garden";
        BookNames[15]="The Alchemist";
        BookNames[16]="Catch-22";
        BookNames[17]="A Thousand Splendid Suns";
        BookNames[18]="The Count of Monte Cristo";
        BookNames[19]="Don Quixote";
    }

    //This method displays the menu of the program and calls appropriate method depending on choice of user
    public void menu(Movie m[],User u[])
    {
        int choice=0;
        User result;
        try{
             do{
                  System.out.println("\n\nPlease enter your choice:");
                  System.out.println("1. View list of books\n2. Reserve a book\n3. Check membership details\n" +
                          "4. View movie details\n5. Exit\n");
                  BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                  choice=Integer.parseInt(br.readLine());
                  switch(choice)
                  {
                    case 1:    viewList();  //To view list of books
                               break;

                    case 2:    result=authenticateUser(u);  //authenticates user
                               if(result!=null)
                                  reserveBook();       //book can be reserved only by authenticated user
                               break;

                    case 3:    result=authenticateUser(u);
                               checkMemDetails(result);    //checks membership details of user
                               break;

                    case 4:    System.out.println("Enter the name of the movie: ");
                               br=new BufferedReader(new InputStreamReader(System.in));
                               viewMovieDetails(m,convertStreamToString(br));     //view movie details
                               break;

                    case 5:    break;

                    default: System.out.println("\nSelect a valid option.\n");
                  }
                }while(choice!=5);

            }
        catch(IOException ie)
        {
          ie.printStackTrace();
        }
        catch(NullPointerException npe)
        {
            npe.printStackTrace();
        }
    }

    //This method authenticates the user
    public User authenticateUser(User u[])
    {
      String uname, pwd;

      System.out.println("\nEnter your username: ");
      BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
      uname=convertStreamToString(br);

      System.out.println("\nEnter your password: ");
      br=new BufferedReader(new InputStreamReader(System.in));
      pwd=convertStreamToString(br);

      User res;
      res = checkUserList(u, uname, pwd);
      if(res==null)   //User not registered
      {
          System.out.println("\nIncorrect username or password!!\n");
      }
      return(res);
}

    //This method checks for user credentials entered against those of registered users
    public User checkUserList(User u[], String uname, String pwd)
    {
       for(int i=0;i<5;i++)
       {
          if(uname.equals(u[i].uname))
          {
             if(pwd.equals(u[i].pwd))
                return(u[i]);
          }
       }
       return null;
    }

    //This method displays list of books available
    public void viewList()
    {
       for(int i=0;i< 20; i++)
       {
           System.out.println(BookNames[i]);
       }
       System.out.println("\n");
    }

    //This method reserves book for a user
    public void reserveBook()
    {
        try{
             System.out.println("Enter the name of the book to be reserved");
             BufferedReader bkName=new BufferedReader(new InputStreamReader(System.in));
             String Name=convertStreamToString(bkName);
             if(bookPresent(Name)==1)
             {
               System.out.println("Thank You! Enjoy the book.");
             }
             else
             {
               System.out.println("Sorry we don't have that book yet.");
             }
           }
           catch(Exception e)
           {
              e.printStackTrace();
           }
    }

   //This method checks membership details of user
    public void checkMemDetails(User u)
    {
        String col1="Name",col2="Email ID",col3="Phone No";
        if(u==null)
           System.out.println("Please talk to a Librarian. Thank you.\n\n");
        else
        {
            System.out.format("\n%-20s%-20s%-15s", col1, col2, col3);
            System.out.format("\n%-20s%-20s%-15s",u.name, u.emailID, u.phno);
        }
    }

    //This method checks if the book requested is available or not
    public int bookPresent(String bkName)
    {
       int i=0;
       for(i=0;i<20;i++)
       {
           if(bkName.equals(BookNames[i]))
               return(1);
       }
       return(0);
    }

    //This method converts BufferedReader to String
    public String convertStreamToString(BufferedReader is)
    {
       java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\n");
       return s.hasNext() ? s.next() : "";
    }

    //This method checks if the movie requested is available or not
    public int moviePresent(Movie m[],String movName)
    {
        for(int i=0;i<15;i++)
        {
            if(movName.equals(m[i].movieName) )
            return(i);
        }
        return(-1);
    }

    //This method displays the details of a movie to the user
    public void viewMovieDetails(Movie m[],String movieName)
    {
       String format="%-40s%-6d%-30s%-3s\n";
       String col1="Movie", col2="Year", col3="Director", col4="Rating";
       int k=moviePresent(m,movieName);
        if(k!=-1)
        {
            System.out.format("\n%-40s%-6s%-30s%-3s\n", col1, col2, col3, col4);
            System.out.format(format, m[k].movieName, m[k].yrOfRelease, m[k].director, m[k].rating);
        }
        else
            System.out.println("\nSorry, the movie you requested is not available!");
    }
}





